Learn 
Functional Programming
(by implementing a SQL-like DSL with _.js and f_.js)

@KrisJordan

Friday, July 20, 12

This is an introductory functional programming talk aimed at programmers who are comfortable with imperative style, perhaps 
object-oriented style, programming and want to learn functional programming.

Objectives

1. Grok functional programming fundamentals

2. Transfer SQL familiarity to Functional Programming

3. Exposure to underscore.js & f_underscore.js

Friday, July 20, 12

@KrisJordan

Why learn functional 
programming?

Friday, July 20, 12

It will change your life.

Learning
functional programming 
will change the way
you design programs.

Friday, July 20, 12

It will change your life.

Why is it 
hard frustrating
to learn functional 
programming?

Friday, July 20, 12

Lists of 
simple 
functions 
aren’t 
exciting.

Friday, July 20, 12

Dude is telling you functional programming has changed my life. Changed the way I program. Underscore.js is amazing. So you 
open up the website and you see a list of simple functions. “MAX”. So you’re thinking: “What is this guy smoking?”

until you see them 
come together.

Friday, July 20, 12

Unix reference. Functional belief that there’s more value in having 100 functions that can operate on 1 data type, than 10 
functions that can operate on 10 data types.

http://www.ﬂickr.com/photos/comunicati/6631664617/

It’s hard to get excited about lists of simple functions...Our Goal

select([“author”,
        “changes”, 
        “sha”],
orderBy(“author”,
where(greaterThan(get(“changes”),5),
from(commits))));

Friday, July 20, 12

6 function calls that are composed together and would be super simple to tweak and play around with.

Very declarative like SQL.

@KrisJordan

Why is it  hard frustrating to learn functional programming?

Academic, 
Mathematical Roots

can lead to

Pedantic 
Resources

Friday, July 20, 12

Alonzo Church - 30s - Lambda Calculus
John McCarthy - 50s - Lisp

Why is it  hard frustrating to learn functional programming?

Imperative / OOP
Programmer
the
resources
you
want

Functional
Programmer

Sane
Person

most
resources

Friday, July 20, 12

Once you’ve learned imperative, object-oriented programming there’s no going back to being a sane person. 

So this talk is 
gonna be filthy.

You will see...

•Imperative Code
•Loops
•State

http://www.ﬂickr.com/photos/jramspott/7355480358/

Friday, July 20, 12

So, what is
“Functional Programming”
?

Friday, July 20, 12

@KrisJordan

Functions are values.

Friday, July 20, 12

@KrisJordan

Functions are values.

•Variables can assigned functions
•Functions can be passed as arguments
•Functions can be return values
•...and one more important thing we’ll get to

Friday, July 20, 12

Variables can be assigned functions.

var sayHello = function() {

  console.log(“Hello, world.”);
};

sayHello();

=> Hello, world.

Friday, July 20, 12

Functions are values.

•Variables can assigned functions
•Functions can be passed as arguments
•Functions can be return values
•...and one more important thing we’ll get to

Friday, July 20, 12

jQuery’s API is awesome thanks to this.

$(“h1”).click(function(){
  console.log(“Hello, world”);
});

 $(“p”).each(function(){
  $(this).wrap(“<marquee/>”);
 });

Friday, July 20, 12

@KrisJordan

Functions can be passed to functions.

var sayHello = function() {

  console.log(“Hello, world.”);
};

var executor = function(aFunction){
  aFunction();
};
executor(sayHello);

=> Hello, world.

Friday, July 20, 12

Functions are values.

•Variables can assigned functions
•Functions can be passed as arguments
•Functions can be return values
•...and one more important thing we’ll get to

Friday, July 20, 12

Functions can be return values.

var say = function(message) {

  return function() {

    console.log(message);
  };
};

var sayHello = say(“Hello, world.”);

sayHello();

=> Hello, world.

Friday, July 20, 12

Functions are values.

•Variables can assigned functions
•Functions can be passed as arguments
•Functions can be return values
•...and one more important thing we’ll get to

Friday, July 20, 12

Functions
are the smallest complete unit of
functionality.

Friday, July 20, 12

Let’s Implement our SQL-like DSL

Friday, July 20, 12

Why? Because it’s a familiar problem. It’s useful to be able to work with data pulled from APIs without shoving it in a DB. Clients 
are fast, APIs don’t always give you what you want.

@KrisJordan

Input data pulled from GitHub API

var commits = [
  {
    "author":"jashkenas",
    "sha":"f6f9d37",
    "message":"Merge branch 'master'",
    "changes":38,
    "additions":19,
    "deletions":19
  },
  ...
];

Friday, July 20, 12

@KrisJordan

Our SQL Target

SELECT author, sha, changes

FROM commits

WHERE changes > 5

ORDER BY author

Friday, July 20, 12

@KrisJordan

Which Step
Should We Choose First?

1. SELECT author, sha, changes

2. FROM commits

3. WHERE changes > 5

4. ORDER BY author

Friday, July 20, 12

We’re talking about a pipeline of work, so the order in which we choose to execute matters. We’re playing the role of SQL query 
optimizer.

@KrisJordan

Let’s Start Here

SELECT author, sha, changes

FROM commits

WHERE changes > 5

ORDER BY author

Friday, July 20, 12

How would we implement this step imperatively?

We’ve got an array of objects. We can write a function that returns a new array of objects where its changes property is greater 
than 5. Easy.

@KrisJordan

Imperative Where

Friday, July 20, 12

Be sure to take time walking through code.

Imperative Where

FROM commits

FROM commits

WHERE changes > 5

WHERE deletions = 0

Friday, July 20, 12

Imperative Where

WHERE changes > 5

WHERE deletions = 0

Friday, July 20, 12

This smells.
 - Code duplication.
 - Conﬂating generic algorithm with speciﬁc application-level request

How can we refactor this? Let’s turn back to our functional bag of tricks.

Functions are values.

•Variables can assigned functions
•Functions can be passed as arguments
•Functions can be return values
•...and one more important thing we’ll get to

Friday, July 20, 12

Let’s refactor to a higher-order function.

Friday, July 20, 12

So we can start by pulling our singular concern out from our where algorithm. We’ve broken things. Where do we get the commit 
object from in the comparison?

Let’s refactor to a higher-order function.

Friday, July 20, 12

We wrap it in a function whose argument will provide it. We need to be sure to return the evaluation, too, and that it returns a 
boolean. This is called a predicate function.

So how do we plug our individual concern back in to our where algorithm? Remember, functions are just values. We can pass this 
gt5Changes function into our where.

Let’s refactor to a higher-order function.

Friday, July 20, 12

Take time explaining this. Walk through.

Everything is happy again.

We’ve abstracted a generalized ﬁltering algorithm for a collection from the criteria we ﬁlter with for a single item by making it 
possible to plugin logic.

Abstract generic algorithms 
from singular concerns.

Pass singular concerns 
in as “iterator” functions.

Friday, July 20, 12

@KrisJordan

underscore.js
60-some classic functions

Friday, July 20, 12

Underscore is a library of 60-some useful functions just like this. Reject (inverse of ﬁlter), all, any, map, reduce. These collection 
functions are higher order functions that abstract out a generic algorithm and allow you to plugin 

Jeremy Ashkenas describes it as the tie to jQuery’s tux.

@KrisJordan

Why underscore.js?

• MIT License
• Simple, high quality, lightweight library from Jeremy Ashkenas
• Most depended upon node.js package manager (npm) package
• 9% of npm published packages depend on _
• Packaged with backbone.js so all backbone collections have 
underscore.js functions built-in

https://github.com/documentcloud/underscore/

Friday, July 20, 12

Now let’s refactor to use underscore.js

Friday, July 20, 12

There’s a function for that. It’s called ﬁlter.

Demo

Friday, July 20, 12

There’s something 
unsatisfying about 
this.

Friday, July 20, 12

How can we do a better job making the singular item logic easier to specify?

This is lame because the property we’re reading and the value 5 are hard coded.

Why can’t we change the signature? Because the signature is what the algorithm expects.

Functions are values.

•Variables can assigned functions
•Functions can be passed as arguments
•Functions can be return values
•...and one more important thing we’ll get to

Friday, July 20, 12

Something smells ﬁshy,
doesn’t `message` pop off the stack?

var say = function(message) {

  return function() {

    console.log(message);
  };
};

var sayHello = say(“Hello, world.”);

sayHello();

Friday, July 20, 12

Functions are values.

•Variables can assigned functions
•Functions can be passed as arguments
•Functions can be return values
•Functions can be “closures”

Friday, July 20, 12

The one more important thing about functional programming in JavaScript is that functions can be closures.

Functions can be closures.
Closures “trap” var refs into a function’s scope.

Friday, July 20, 12

Closures in OOP terms:
native, minimal
Command pattern

OOP

class MsgCommand {
  private _msg;
  function MsgCommand(msg) {
    this._msg = msg;
  }
  function exec() {
    print this._msg;
  }
}

cmd = new MsgCommand(“OOP”);
cmd.exec();

FP

var msgCommand = function(msg) {
  return function() {
    console.log(msg);
  };
};

cmd = msgCommand(“FP”);
cmd();

Friday, July 20, 12

@KrisJordan

Let’s refactor to generate a closure.

Friday, July 20, 12

Plural `where` Function

             where(             , commits);

Friday, July 20, 12

@KrisJordan

Anonymous Function

Plural `where` Function

where(function(commit){

  return commit.changes > 5;

},commits);

Friday, July 20, 12

We skipped over this before, but you can write functions anonymously in JavaScript and pass them as values. Just like you can 
hard code constants. In general it’s not a good idea. It’s hard to read and hard to test. jQuery apps tend to abuse the simplicity of 
this.

@KrisJordan

Function Variable

Anonymous Function

Plural `where` Function

var gt5Changes = function(commit){
  return commit.changes > 5;
};
where(gt5Changes, commits);

Friday, July 20, 12

We had already been using a function variable but this is an interesting refactoring to make note of.

@KrisJordan

Function Variable

Anonymous Function

Plural `where` Function

where(greaterThan(“changes”, 5), commits);

@KrisJordan

Friday, July 20, 12

Closure Generator

Function Variable

Anonymous Function

Plural `where` Function

var greaterThan = function(prop, value){
  return function(item){
    return item[prop] > value;
  };
};
where(greaterThan(“changes”, 5), commits);

Friday, July 20, 12

Closure Generator

Function Variable

Anonymous Function

Plural `where` Function

var greaterThan = function(prop, value){
  return function(item){
    return item[prop] > value;
  };
};
where(greaterThan(“changes”, 5), commits);

“Trapped” in 
Scope by
Closure

Friday, July 20, 12

For OOP folks closures are like the command pattern.

Demo

Friday, July 20, 12

Show this refactoring.

f_underscore.js

• Companion library to underscore.js for welding and weilding 
iterator functions
• Dynamically generate expressions and predicate closures
• MIT licensed
• http://krisjordan.github.com/f_underscore/

Friday, July 20, 12

Turns out you can do this for complete expressions.

Now let’s refactor to use f_underscore.js

var greaterThan = f_.greaterThan,
    get = f_.get;

where(greaterThan(get(“changes”),5), commits);

Friday, July 20, 12

Note: we’ll come back to `get`

Why `get`?

var get = function(prop) {
        return function(obj) {
          return obj[prop];
        };
    };

greaterThan(get(“additions”),get(“deletions”)), commits);

Friday, July 20, 12

What’s left?

select([“author”,
        “changes”, 
        “sha”],
orderBy(“author”,
where(greaterThan(get(“changes”),5),
from(commits))));

`from` is _.identity

Friday, July 20, 12

What’s left?

orderBy(“author”, commits);

Friday, July 20, 12

What’s left?

orderBy(“author”, commits);

var orderBy = function(field, commits) {
  return _.sortBy(commits, f_.get(field));
};

Friday, July 20, 12

What’s left?

select([“author”,“changes”,“sha”], commits);

Friday, July 20, 12

map(

3

2

1

,

function(item){
 return item * item;
}

);

Friday, July 20, 12

@KrisJordan

map(

3

2

1

,

function(item){
 return item * item;
}

);

Friday, July 20, 12

@KrisJordan

map(

3

2

1

,

function(item){
 return item * item;
}

);

9

Friday, July 20, 12

@KrisJordan

map(

3

2

1

,

function(item){
 return item * item;
}

);

9

4

Friday, July 20, 12

@KrisJordan

map(

3

2

1

,

function(item){
 return item * item;
}

);

9

4

1

Friday, July 20, 12

@KrisJordan

map(

3

2

1

,

function(item){
 return item * item;
}

);

return to
caller

9

4

1

Friday, July 20, 12

@KrisJordan

What’s left?

select([“author”,“changes”,“sha”], commits);

var select = function(fields, commits) {
  return _.map(commits, f_.project(fields));
};

Friday, July 20, 12

Complete Implementation of DSL

var greaterThan = f_.greaterThan,
    get = f_.get,
    select = function(fields, commits) {
      return _.map(commits, f_.project(fields));
    },
    orderBy = function(field, commits) {
      return _.sortBy(commits, f_.get(field));
    },
    where = function(predicate, data) {
      return _.filter(data, predicate);
    },
    from = _.identity
    ;

Friday, July 20, 12

If you’re looking at this thinking, but wait, isn’t all we did here alias and change the order of parameters? You’re right.

Demo

Friday, July 20, 12

If you’re looking at this thinking, but wait, isn’t all we did here alias and change the order of parameters? You’re right.

Our “DSL”

Idiomatic

select([“author”,
        “changes”, 
        “sha”],
orderBy(“author”,
where(
  greaterThan(
   get(“changes”),5
  ),
from(commits))));

_(commits)
 .chain()
 .filter(f_(“changes”).gt(5))
 .sortBy(f_(“author”))
 .map(f_.project(
   [“author”,
    ”sha”,
    ”changes”]))
 .value();

Friday, July 20, 12

Recap

• Functions are data
• Refactor to functions as arguments

• Abstract “plural” algorithm away from “singular” logic
• Pass “singular” functions in

• Refactor to function/closure generators

• Poor man’s macro
• Parameterize iterator functions

• Check out underscore.js and f_underscore.js

@KrisJordan

Friday, July 20, 12

•

•

Functions are data

Closures “trap” variable references in scope, useful for generating functions

• Write singular function generators, leverage with plural executor functions

•

Underscore provides classic functions that are widely useful

Questions?

Friday, July 20, 12

Friday, July 20, 12

